<%
/*
 * Copyright (c) 2011 Metrum Research Group LLC. All Rights Reserved.
 *
 * You may use this software under the terms of GPL v3 (http://www.gnu.org/licenses/gpl.html).
 *
 * METRUM RESEARCH GROUP LLC MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. METRUM RESEARCH GROUP LLC SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */
%>
<!-- taglib provides the r:R tag functionality.-->
<%@ taglib prefix="r" uri="/WEB-INF/RTags.tld" %>
<!-- page import provides java and sumo libraries. -->
<%@ page import="java.io.*" %>
<%@ page import="sumo.*" %>
<!-- params2r causes request parameters to be assigned in the R session. -->
<jsp:include page="params2r.jsp" />
<!-- ExpressionEvaluator allows assignment of R results in the jsp page context (R.evaluate()). -->
<%ExpressionEvaluator R = (ExpressionEvaluator) session.getAttribute("evaluator");%>
<!-- Here we capture any parameters we expect from the user. -->
<%
    String grp = request.getParameter("grp") ;
    String condition = request.getParameter("condition") ;
    String scale = request.getParameter("scale") ;
    String span = request.getParameter("span") ;
    String directory = R.evaluate("writeLines(getwd())",false);
%>
<!-- Here we supply defaults for any parameters the user did not specify. -->
<!-- Note the independence of the R session and the page context. -->
<!-- Note type of argument may depend on context. -->
<%
    if (grp == null) grp = "all";
    if (condition == null) condition = "all";
    if (scale == null) scale="linear";
    if (span == null) span = "2/3";
    if (grp.equals("all")){R.evaluate("grp <- 'all'");}else{R.evaluate("grp <- 'Subject'");}
    if (condition.equals("all")){R.evaluate("condition <- 'all'");}else{R.evaluate("condition <- 'Subject'");}
    if (scale.equals("linear")){ R.evaluate("log <- FALSE");}else{R.evaluate("log <- TRUE");}
    R.evaluate("span <- " + span);
%>
<!-- Here we make some flags to set defaults in the forms appropriately. -->
<%
    String linearChecked = "";
    String logChecked = "";
    if (scale.equals("linear")){linearChecked = "checked";}else{logChecked = "checked";}
    String allCondition = "";
    String SubjectCondition = "";
    if (condition.equals("all")){allCondition = "checked";}else{SubjectCondition = "checked";}
    String allSelected = "";
    String SubjectSelected = "";
    if (grp.equals("all")){allSelected = "selected='yes'";}else{SubjectSelected = "selected='yes'";}
%>
<title>Sumo Evaluate</title>
<!-- Here we execute an extended block of R code. -->
<r:R silent="true">
library(lattice)
Theoph$all <- 1
png('theoph.png')
xyplot(
	conc~Time|Theoph[[condition]],
	Theoph,
	groups=Theoph[[grp]],
	scales=list(log=log),
	type=c('p','smooth'),
	span=span
)
dev.off()                                                                                                    
</r:R>

<img src='SpecificImage?file=<%=directory%>/theoph.png'/>
<form 
    name='theoph' 
    action='evaluate.jsp' 
    method='get'
>
    <select name='grp'> 
        <option value='all' <%=allSelected%>>ungrouped points</option>
        <option value='Subject' <%=SubjectSelected%>>points grouped by subject</option>
    </select>
    <br/>
    conditioning: 
    none<input 
        type='radio' 
        name='condition' 
        value = 'all' <%=allCondition%>  
    />
    by subject<input 
        type='radio' 
        name='condition' 
        value = 'Subject' <%=SubjectCondition%>  
    /><br/>
    scale: 
    linear<input 
        type='radio' 
        name='scale' 
        value='linear' <%=linearChecked%>
    />
    log<input 
        type='radio' 
        name='scale' 
        value='log' <%=logChecked%>
    />
    <br/>
    smoothing span: 
    <input 
        type='text' 
        name='span' 
        value = '<%=span%>'  
    /><br/>
    <input 
        type='submit' 
        value='evaluate'
    />
</form>
<pre>
  <r:R>
    head(Theoph)
  </r:R>
</pre>

